<?php

require_once('Myzen/Oauth/Datastore/Interface.php');

class Myzen_Oauth_Datastore_Filesystem implements Myzen_Oauth_Datastore_Interface {

  /* location of the file system */
  protected $_filesystemFolder = '../app/oauthfilestore/'; 
  
  function lookup_consumer($sentConsumerKey) {
  	
  	$handle = @fopen($this->_filesystemFolder.'consumerdetails.txt', "r");
	if ($handle) {
      while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        list($consumerKey,$consumerSecret) = split(':',$buffer);
    	
    	/* have to remove the line feed from the secret */
    	$consumerSecret = preg_replace('/\n/','',$consumerSecret);
    	
        if($sentConsumerKey == $consumerKey) {
          fclose($handle);
          return new Myzen_Oauth_Consumer($consumerKey,$consumerSecret);
        }
      }
    }
    
    
  }

  function lookup_token($consumer, $token_type, $token) {
  	
  }

  function lookup_nonce($consumer, $token, $nonce, $timestamp) {
  	
  }

  function new_request_token($consumer){
    $consumerKey = $consumer->key;
    $requestToken = md5(time().'some random sentence'.rand());
    $requestTokenSecret = md5('another random sentence'.rand().time());

  	$handle = @fopen($this->_filesystemFolder.$consumerKey.'/requestToken/'.$requestToken, "w");
  	fwrite($handle,$requestTokenSecret);
    fclose($handle);  	    

    return 'oauth_token='.$requestToken.'&oauth_token_secret='. $requestTokenSecret;  	
  }

  function new_access_token($token, $consumer) {
  	
  }

}